<template>
  <div class="GoodsRefer">
    <p class="title">不同角度查看商品</p>
    <div class="box">
        <!-- 小盒子 -->
        <div class="small_box" ref="small" @mouseleave="hide">
            <div class="event" @mousemove="handler"></div>
            <img :src="goodsList.goodsRelateimg[curIndex]" alt="">
            <span class="mask" ref="mask"></span>
        </div>
        <div class="big_box" ref="big_box">
            <img :src="goodsList.goodsRelateimg[curIndex]" alt="" ref="big">
        </div>
    </div>
    <div class="list">
        <ul>
            <li v-for="(goods,index) in goodsList.goodsRelateimg" :key="index" @click="changeCurIndex(index)"><img :src="goods" alt=""></li>
            <!-- <li><img src="../img/pic0001.jpg" alt=""></li>  -->
        </ul>
    </div>
  </div>
</template>

<script>
    export default {
        name:'GoodsRefer',
        props:['goodsList'],
        data() {
            return {
                curIndex:0,
            }
        },
        mounted() {
            
        },
        methods: {
            handler(event){
                let small = this.$refs.small
                let mask = this.$refs.mask
                let big = this.$refs.big
                let bigBox = this.$refs.big_box
                let left = event.offsetX - mask.offsetWidth/2
                let top = event.offsetY - mask.offsetHeight/2
                if(left<=0) left = 0
                if(left >= small.offsetWidth - mask.offsetWidth) left=small.offsetWidth - mask.offsetWidth
                if(top<=0) top = 0
                if(top >= small.offsetHeight - mask.offsetHeight) top = small.offsetHeight - mask.offsetHeight
                // 修改元素的left|top
                mask.style.left = left + 'px'
                mask.style.top = top + 'px'
                // big.style.left = -2*left + 'px'
                big.style.left  = -left/(small.offsetWidth / big.offsetWidth) + 'px'
                big.style.top = -top/(small.offsetHeight / big.offsetHeight) + 'px'
                bigBox.style.display = "block"
            },
            hide(){
                let bigBox = this.$refs.big_box
                bigBox.style.display = "none"
            },
            changeCurIndex(index){
                this.curIndex = index
            },
        },
    }
</script>

<style scoped>
    .GoodsRefer{
        width: 100%;
        height: 600px;
        padding: 20px;
        
    }
    .title{
        width: 100%;
        height: 60px;
        line-height: 56px;
        font-size: 20px;
        padding-left: 20px;
        font-family: "楷体";
        font-weight: bold;
        color: #333;
        border-bottom: 4px solid skyblue;
    }
    .box{
        width: 350px;
        height: 350px;
        margin: 50px 0 0 50px;
        
        position: relative;

    }
    .small_box{
        width: 100%;
        height: 100%;
        border: 1px solid #ccc;
        box-sizing: border-box;
        position: relative;
    }
    .small_box div{
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
    }
    .small_box img{
        width: 100%;
        height: 100%;
    }
    .mask{
        width: 100px;
        height: 100px;
        background-color: rgba(255, 255, 0, .3);
        position: absolute;
        left: 0;
        top: 0;
        cursor: move;
        /*隐藏*/
        display: none;
    }
    .small_box:hover .mask{
        display: block;
    }
    .big_box{
        width: 600px;
        height: 600px;
        border: 1px solid #ccc;
        position: absolute;
        left: 360px;
        top: 0;
        overflow: hidden;
        display: none;
    }
    .small_box:hover .big_box{
        display: block;
    }
    .big_box img{
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
    }
    .list{
        margin: 5px 0 0 50px;
        width: 350px;
        height: 52px;
        border: 1px solid #ccc;
    }
    .list ul{
        width: 100%;
        height: 50px;
        vertical-align: middle;
    }
    .list ul li{
        width: 50px;
        height: 50px;
        float: left;
        cursor: pointer;
    }
    .list ul li img{
        width: 100%;
        height: 100%;
    }
</style>